<?php
/**
 * Project: Recipe
 * File: VideoArticleAction.class.php
 * Author: Xnfy520@gmail.com
 * Date: 12-5-5
 * Time: 下午2:09
 */


class VideoArticleAction extends CommonAction{

	function index(){

		import("ORG.Util.Page");

		$videocategory = M('VideoCategory');
		$videocategoryinfo = $videocategory->field('id,name')->select();
		$this->assign('videocategoryinfo', $videocategoryinfo);

		$videoarticle = D('VideoArticle');

		if(isset($_POST['search'])){
			if(!empty($_POST['key'])){
				switch($_POST['search']){
					case 1 :
						$datas['id'] = $_POST['key'];
						break;
					case 2:
						$datas['name'] = array("like","%{$_POST['key']}%");
						break;
					case 3 :
						$datas['alias'] = array('like',"%{$_POST['key']}%");
						break;
				}
				if(isset($_GET['sid']) && isset($_GET['cid'])){
					$datas['sectionid'] = $_GET['sid'];
					$datas['categoryid'] = $_GET['cid'];
				}
				$total = $videoarticle->where($datas)->count();
				$Page = new Page($total, C('PAGENUM'));
				$show = $Page->show();
				$list = $videoarticle->relation(true)->where($datas)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
			}else{
				$this->error('搜索内容不能为空', __APP__.'/VideoArticle/index');
			}
		}else{

			if(!isset($_GET['sid']) && !isset($_GET['cid'])){
				$total = $videoarticle->count();
				$Page = new Page($total, C('PAGENUM'));// 实例化分页类 传入总记录数和每页显示的记录数
				$show = $Page->show();// 分页显示输出
				$list = $videoarticle->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
			}elseif(isset($_GET['sid']) && isset($_GET['cid'])){
				$datas['sectionid'] = $_GET['sid'];
				$datas['categoryid'] = $_GET['cid'];
				$total = $videoarticle->where($datas)->count();
				$Page = new Page($total, C('PAGENUM'));// 实例化分页类 传入总记录数和每页显示的记录数
				$show = $Page->show();// 分页显示输出
				$list = $videoarticle->relation(true)->where($datas)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
			}elseif(isset($_GET['sid'])){
				$total = $videoarticle->where('sectionid='.$_GET['sid'])->count();
				$Page = new Page($total, C('PAGENUM'));// 实例化分页类 传入总记录数和每页显示的记录数
				$show = $Page->show();// 分页显示输出
				$list = $videoarticle->relation(true)->where('sectionid='.$_GET['sid'])->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
			}

		}

		$this->assign('list',$list);// 赋值数据集

		$this->assign('show',$show);// 赋值分页输出

		$this->display('index');
	}

	/**
	 * 指向添加视频界面
	 */
	function add(){
		$saucecategory = M('SpecialCategory');
		$data = $saucecategory->select();
		$this->assign('data', $data);
		$this->display();
	}

	/**
	 * 添加视频
	 */
	function insert(){
		$videoarticle= D('VideoArticle');
		$tmpdir = './Public/Content/tmp/';
		$srcdir = './Public/Content/VideoArticle/';
		if($data = $videoarticle->create()){
			if($videoarticle->add()){
				if(!empty($data['image'])){
					copy($tmpdir.$data['image'], $srcdir.$data['image']);
					copy($tmpdir.'thumb_'.$data['image'], $srcdir.'thumb_'.$data['image']);
					if($dirsrc = opendir($tmpdir)){
						while($files = readdir($dirsrc)){
							if($files!='.' && $files!='..'){
								$pathfile = $tmpdir.$files;
								if(is_file($pathfile)){
									unlink($pathfile);
								}
							}
						}
					}
				}
				$this->success('添加视频成功', __APP__.'/VideoArticle/index');
			}else{
				$this->error('添加视频失败');
			}
		}else{
			$this->error($videoarticle->getError());
		}
	}

	/**
	 *视频图片上传
	 */
	function uploads(){
		import("ORG.Net.emit_upload");
		import("ORG.Util.emit_image");
		$upload = new upload(array("filepath"=>"./Public/Content/tmp","allowtype"=>array('gif','png','jpg'),"israndname"=>true,"maxsize"=>2000000));
		if($upload->uploadfiles('pic')){
			$img = new image("./Public/Content/tmp");
			$imgname =  $upload->getnewname();
			if($img->cut($imgname)){
				$img->thumb($imgname, 210, 210);
				$img->thumb($imgname, 70, 70, 'thumb_');
			}
			echo $upload->getnewname();
		}else{
			echo $upload->geterrormsg();
		}
	}

	/**
	 * 指向修改视频界面
	 */
	function edit(){
		if(isset($_GET['sid']) && !empty($_GET['sid'])){
			if(isset($_GET['cid']) && !empty($_GET['cid'])){
				if(isset($_GET['id']) && !empty($_GET['id'])){

					$saucecategory = M('SpecialCategory');
					$saucecategoryinfo = $saucecategory->field('id,name')->select();
					$this->assign('saucecategory', $saucecategoryinfo);

					$foodcategory = M('FoodCategory');
					$foodcateforyinfo = $foodcategory->field('id,name')->where('saucecategoryid='.$_GET['sid'])->select();
					$this->assign('foodcategory', $foodcateforyinfo);

					$videoarticle = M('VideoArticle');
					$videoarticleinfo = $videoarticle->find($_GET['id']);
					$this->assign('data', $videoarticleinfo);

					$this->display();
				}else{
					$this->error('异常操作');
				}
			}else{
				$this->error('异常操作');
			}
		}else{
			$this->error('异常操作');
		}
	}

	/**
	 * 修改视频
	 */
	function update(){
		if(isset($_POST['id']) && !empty($_POST['id'])){
			$tmpdir = './Public/Content/tmp/';
			$srcdir = './Public/Content/VideoArticle/';
			$videoarticle= M('VideoArticle');
			$videoarticleinfo = $videoarticle->find($_POST['id']);
			if($data = $videoarticle->create()){
				if($videoarticle->save()){
					if($data['image']!=$videoarticleinfo['image']){
						unlink($srcdir.$videoarticleinfo['image']);
						unlink($srcdir.'thumb_'.$videoarticleinfo['image']);
						copy($tmpdir.$data['image'], $srcdir.$data['image']);
						copy($tmpdir.'thumb_'.$data['image'], $srcdir.'thumb_'.$data['image']);
						if($dirsrc = opendir($tmpdir)){
							while($files = readdir($dirsrc)){
								if($files!='.' && $files!='..'){
									$pathfile = $tmpdir.$files;
									if(is_file($pathfile)){
										unlink($pathfile);
									}
								}
							}
						}
					}
					$this->success('修改视频成功',  __APP__.'/VideoArticle/index/sid/'.$data['sectionid'].'/cid/'.$data['categoryid']);
				}else{
					$this->error('修改视频失败');
				}
			}else{
				$this->error($videoarticle->getError());
			}
		}else{
			$this->error('异常操作');
		}
	}

	/**
	 * 删除视频
	 */
	function del(){
		if(!empty($_POST['actions'])){
			$srcdir = './Public/Content/VideoArticle/';
			$videoarticle= M('VideoArticle');
			foreach($_POST['actions'] as $key=>$value){
				$videoarticleinfo = $videoarticle->find($value);
				unlink($srcdir.$videoarticleinfo['image']);
				unlink($srcdir.'thumb_'.$videoarticleinfo['image']);
				$num = $videoarticle->delete($value);
			}
			if($num>0){
				$this->success('删除视频成功', __APP__.'/VideoArticle/index/sid/'.$videoarticleinfo['sectionid'].'/cid/'.$videoarticleinfo['categoryid']);
			}else{
				$this->error('删除视频失败');
			}
		}else{
			$this->error('请先选择视频');
		}
	}

	function returns(){
		$foodcategory = M('FoodCategory');
		$list = $foodcategory->field('id,name')->where('saucecategoryid='.$_POST['id'])->select();
		$data = array();
		foreach($list as $key=>$value){
			$data[] = $value;
		}
		echo json_encode($data);
	}

}